package com.ican.dao.rule;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.ican.common.SystemConstants;
import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.rule.CategoryRule;

@Repository("categoryRuleDao")
public class CategoryRuleDao extends HibernateDao<CategoryRule, Integer> {
    
    public Page<CategoryRule> findAllCategory(Page<CategoryRule> page, CategoryRule category){
        List<String> params = new ArrayList<String>();
        if(category != null){
            if(category.getName() != null && category.getName().length() > 0){
                params.add("name like '%" + category.getName() + "%'");
            }
            if(category.getUrl() != null && category.getUrl().length() > 0){
                params.add("url like '%" + category.getUrl() + "%'");
            }
            if(category.getWebsiteId() != null){
                params.add("websiteId = " + category.getWebsiteId());
            }
            if(category.getRuleId() != null){
                params.add("ruleId = " + category.getRuleId());
            }
            if(category.getDataStatus() == 0 || category.getDataStatus() == 1){
                params.add("dataStatus = " + category.getDataStatus());
            }
            if(category.getScanStatus() != null && category.getScanStatus() >= 0){
                params.add("scanStatus = " + category.getScanStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from CategoryRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.findPage(page, sb.toString());
    }
    
    public List<CategoryRule> findAllCategory(CategoryRule category){
        List<String> params = new ArrayList<String>();
        if(category != null){
            if(category.getName() != null && category.getName().length() > 0){
                params.add("name like '%" + category.getName() + "%'");
            }
            if(category.getUrl() != null && category.getUrl().length() > 0){
                params.add("url like '%" + category.getUrl() + "%'");
            }
            if(category.getWebsiteId() != null){
                params.add("websiteId = " + category.getWebsiteId());
            }
            if(category.getRuleId() != null){
                params.add("ruleId = " + category.getRuleId());
            }
            if(category.getDataStatus() == 0 || category.getDataStatus() == 1){
                params.add("dataStatus = " + category.getDataStatus());
            }
            if(category.getScanStatus() != null && category.getScanStatus() >= 0){
                params.add("scanStatus = " + category.getScanStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from CategoryRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.find(sb.toString());
    }
    
    public boolean categoryCodeIsExist(String code){
        Object obj = this.findUnique("select count(id) from CategoryRule where code = '" + code + "'");
        if(obj != null && Integer.parseInt(obj.toString()) > 0){
            return true;
        }
        return false;
    }
    
    public void addCategory(CategoryRule category){
        category.setScanStatus(SystemConstants.RULE_CONFIG_SCAN_STATUS_NO);
        category.setCreateTime(new Date());
        category.setModifyTime(new Date());
        this.save(category);
    }
    
    public void modifyCategory(CategoryRule category){
        if(category == null || category.getId() == null){
            return;
        }
        CategoryRule entity = this.get(category.getId());
        if(entity == null){
            return;
        }
        if(category.getDataStatus() == 0 || category.getDataStatus() == 1){
            entity.setDataStatus(category.getDataStatus());
        }
        if(category.getName() != null){
            entity.setName(category.getName());
        }
        if(category.getUrl() != null){
            entity.setUrl(category.getUrl());
        }
        if(category.getDescription() != null){
            entity.setDescription(category.getDescription());
        }
        if(category.getLangId() != null){
            entity.setLangId(category.getLangId());
        }
        if(category.getRuleId() != null){
            entity.setRuleId(category.getRuleId());
        }
        entity.setModifyTime(new Date());
        this.save(entity);
    }
    
    public void updateCategoryScanStatus(Integer cateId, Integer scanStatus){
        StringBuffer sb = new StringBuffer("update CategoryRule set scanTime = :p_scan_time, scanStatus = :p_scan_status where id = :p_id");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("p_scan_time", new Date());
        params.put("p_scan_status", scanStatus);
        params.put("p_id", cateId);
        this.batchExecute(sb.toString(), params);
    }
    
    public void updateCategoryScanStatusAndLastPageUrl(Integer cateId, Integer scanStatus, String lastPageUrl){
        StringBuffer sb = new StringBuffer("update CategoryRule set scanTime = :p_scan_time, scanStatus = :p_scan_status, lastPageUrl = :p_last_page_url where id = :p_id");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("p_scan_time", new Date());
        params.put("p_scan_status", scanStatus);
        params.put("p_last_page_url", lastPageUrl);
        params.put("p_id", cateId);
        this.batchExecute(sb.toString(), params);
    }
    
    @SuppressWarnings("unchecked")
    public List<Integer> findAllCategoryIdToScan(CategoryRule category){
        List<String> params = new ArrayList<String>();
        if(category != null){
            if(category.getWebsiteId() != null){
                params.add("websiteId = " + category.getWebsiteId());
            }
            if(category.getDataStatus() == 0 || category.getDataStatus() == 1){
                params.add("dataStatus = " + category.getDataStatus());
            }
            if(category.getScanStatus() != null){
                params.add("scanStatus = " + category.getScanStatus());
            }
        }
        StringBuffer sb = new StringBuffer("select id from CategoryRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        return this.createQuery(sb.toString()).list();
    }
}
